package acm.集美大学;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class GComapre {
    public static void main(String[] args) {
        int count=10000;
        Random r=new Random();
        while (count-- > 0) {
            String[] arr=new String[5];
            int[]a=new int[5];
            int[]b=new int[5];
            for (int i = 0; i < 5; i++) {
                char[] cs=new char[3];
                for (int j = 0; j < 3; j++) {
                    cs[j]= (char) ('A'+r.nextInt(26));
                }
                arr[i]=new String(cs);
                a[i]=r.nextInt(3);
                b[i]=r.nextInt(3);
                if (b[i]>a[i]){
                    i--;
                }
            }
            List<String> list = fun1(arr.clone(), a.clone(), b.clone());
            List<String> list2 = fun2(arr.clone(), a.clone(), b.clone());
            if (!list2.equals(list)){
                list = fun1(arr.clone(), a.clone(), b.clone());
                list2 = fun2(arr.clone(), a.clone(), b.clone());
                System.out.println();
            }
        }
    }

    static Handle[] handles = new Handle[5];
    static char[] chars = new char[3];

    public static List<String> fun1(String[] strs,int[]a,int[] b) {
        for (int i = 0; i < handles.length; i++) {
            handles[i] = new Handle(strs[i].toCharArray(), a[i], b[i]);
        }
        List<String> list = new ArrayList<>();
        for (char i = 'A'; i <= 'Z'; i++) {
            chars[0] = i;
            for (char j = 'A'; j <= 'Z'; j++) {
                chars[1] = j;
                for (char k = 'A'; k <= 'Z'; k++) {
                    chars[2] = k;
                    boolean flag = true;
                    for (Handle handle : handles) {
                        if (!handle.match(chars)) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) list.add(new String(chars));
                }
            }
        }
        return list;
    }

    static class Handle {
        char[] str;
        int[] count = new int[128];
        int a, b;

        public Handle(char[] str, int a, int b) {
            this.str = str;
            for (char c : str) {
                count[c]++;
            }
            this.a = a;
            this.b = b;
        }

        public boolean match(char[] chars) {
            int cnt = 0;
            for (char c : chars) {
                if (count[c] != 0) {
                    cnt++;
                }
            }
            if (this.a != cnt) return false;
            int b = 0;
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == str[i]) {
                    b++;
                }
            }
            return this.b == b;
        }
    }



    public static List<String> fun2(String[] s, int[]a, int[]b) {
        List<String> li=new ArrayList<>();
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<26;j++)
            {
                for(int k=0;k<26;k++)
                {
                    char aa=(char) (i+'A');
                    char bb=(char) (j+'A');
                    char cc=(char) (k+'A');
                    int count=0;
                    for(int w=0;w<5;w++)
                    {
                        int count1=0;
                        int count2=0;
                        for(int ww=0;ww<3;ww++)
                        {
                            if(s[w].charAt(ww)==aa||s[w].charAt(ww)==bb||s[w].charAt(ww)==cc)
                            {
                                count1++;
                            }
                            if(ww==0&&s[w].charAt(ww)==aa)
                            {

                                count2++;
                            }
                            if(ww==1&&s[w].charAt(ww)==bb)
                            {

                                count2++;
                            }
                            if(ww==2&&s[w].charAt(ww)==cc)
                            {

                                count2++;
                            }
                        }
                        if(count1==a[w]&&count2==b[w])
                            count++;
                    }
                    if(count==5)
                    {
                        String ss=""+aa+bb+cc;
                        li.add(ss);
                    }
                }
            }
        }
        return li;
    }

}
